package com.example.himalaya.view.fragments;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.himalaya.R;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.view.callback.IAnnouncerDetailViewCallback;
import com.example.himalaya.presenters.AnnouncerDetailPresenter;
import com.example.himalaya.view_component.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.List;

public class AnnouncerDetailFragment extends BaseFragment {

    // Other UI object
    private UILoader mUiLoader;

    // UI object
    private View mSuccessView;
    private TextView mAnnouncerDetailtv;

    // Presenter
    private IAnnouncerDetailViewCallback mAnnouncerDetailViewCallback;
    private AnnouncerDetailPresenter mAnnouncerDetailPresenter;
    private Announcer mCurrentAnnouncer;

    @Override
    protected View onSubViewLoaded(final LayoutInflater inflater, ViewGroup container) {

        initUILoader(inflater, container);


        initView();

        return mUiLoader;
    }

    private void initView() {
        mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    private void initUILoader(final LayoutInflater inflater, final ViewGroup container) {
        if (mUiLoader == null) {
            mUiLoader = new UILoader(container.getContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSuccessView(inflater, container);
                }


                @Override
                protected View getEmptyView() {

                    View itemView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);

                    TextView tipsView = itemView.findViewById(R.id.tips_for_empty);

                    tipsView.setText(R.string.tips_for_empty_sub);
                    return itemView;
                }
            };

            if (mUiLoader.getParent() instanceof ViewGroup) {
                ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
            }

        }
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        mSuccessView = layoutInflater.inflate(R.layout.fragment_announcer_details, container, false);

        initSuccessVariable();

        initSuccessAdapter();

        initSuccessPresenter();

        initSuccessListener();

        initSuccessCallBack();

        initSuccessData();

        initSuccessView();

        return mSuccessView;
    }


    private void initSuccessView() {
        mAnnouncerDetailtv.setText(mCurrentAnnouncer.getVdesc());
    }

    private void initSuccessData() {
        mCurrentAnnouncer = mAnnouncerDetailPresenter.getCurrentAnnouncer();
    }

    private void initSuccessListener() {

    }

    private void initSuccessPresenter() {
        mAnnouncerDetailPresenter = AnnouncerDetailPresenter.getInstance();
    }

    private void initSuccessAdapter() {
    }

    private void initSuccessVariable() {
        mAnnouncerDetailtv = mSuccessView.findViewById(R.id.announcer_detail_tv);
    }

    private void initSuccessCallBack() {

        mAnnouncerDetailViewCallback = new IAnnouncerDetailViewCallback() {
            @Override
            public void onDetailListLoaded(List<Track> tracks) {

            }

            @Override
            public void onNetworkError(int errorCode, String errorMsg) {

            }

            @Override
            public void onAnnouncerLoaded(Announcer announcer) {

            }

            @Override
            public void onLoaderMoreFinished(int size) {

            }

            @Override
            public void onRefreshFinished(int size) {

            }
        };

        mAnnouncerDetailPresenter.registerViewCallback(mAnnouncerDetailViewCallback);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mAnnouncerDetailPresenter != null) {
            mAnnouncerDetailPresenter.unRegisterViewCallback(mAnnouncerDetailViewCallback);
        }
    }
}
